<?php
class UserController extends Zend_Controller_Action{
	public function init(){
		$this->registry = Zend_Registry::getInstance();
		$this->view = $this->registry['view'];
		$this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->template = "system";
		$this->view->type = "user";
		$this->view->conf = new Zend_Config(require './application/configs/config.php');
	}

	public function logoutAction(){
		session_unset();
        echo "<script>window.location='../user/login';</script>"; 
	}

      public function loginAction(){
          $filter=new Zend_Filter_StripTags();
            $user = new User();
            if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
                 $username=$filter->filter(($this->_request->getPost('username')));
                 $password=$filter->filter(($this->_request->getPost('password')));
                 $password = md5($password);
              //先根据用户名判断是否存在此人
                 $query = $user->getuserByusername($username);
                 $num = count($query);
                 if($num=="0"){
                     echo "您输入的用户名错误";
                     echo "<script>window.location='../user/login';</script>";
                 }else{
              //用户名存在的话，这去判断用户名和密码是否匹配
                     $result = $user->getuserByusernameAndpassword($username, $password);
                    // echo $result;exit;
                     $count = count($result);
                     if($count=="0"){
                         echo "您输入的用户名或密码错误";
                         echo "<script>window.location='../user/login';</script>";
                     }else{
                         $_SESSION['user'] = $result[0];
                       
                         echo "<script>window.location='../index';</script>";
                     }
                 }
            }

            echo $this->view->render('user/login.html');
      }



    public function addAction(){
            $filter=new Zend_Filter_StripTags();
            $user = new User();
            if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
                 $username=$filter->filter(($this->_request->getPost('username')));
                 $password=$filter->filter(($this->_request->getPost('password')));
                 $password = md5($password);

                 $data = array(
                     'user_name'=>$username,
                     'password'=>$password
                 );
              $user_id = $user->insert($data);
              if($user_id){
                  echo "<script>alert('Success!')</script>";
              }  else {
                  
              }
            }

            echo $this->view->render('user/add.html');
      }

          public function editAction(){
            $filter=new Zend_Filter_StripTags();
            $user = new User();
            $user_id=$filter->filter($this->_request->getParam('user_id'));
            if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
                 $username=$filter->filter(($this->_request->getPost('username')));
                 $password=$filter->filter(($this->_request->getPost('password')));
                 $user_id=$filter->filter(($this->_request->getPost('user_id')));
                 $password = md5($password);

                 $data = array(
                     'user_name'=>$username,
                     'password'=>$password
                 );
              $user_id = $user->updateuserByuser_id($data, $user_id);
              if($user_id){
                  echo "<script>window.location='list'</script>";
              }  else {
                  
              }
            }else{
                $this->view->user = $user->getuserByuser_id($user_id);
            }

            echo $this->view->render('user/edit.html');
      }

      public function deleteAction(){
            $filter=new Zend_Filter_StripTags();
            $user = new User();
            $user_id=$filter->filter($this->_request->getParam('user_id'));

            $query = $user->deluserByuser_id($user_id);
            if($query){
			echo "<script>window.location='list'</script>";
		}else{
			 
		}
      }

      public function listAction(){
            $filter=new Zend_Filter_StripTags();
            $user = new User();
            $group = new Group();
            $page=$filter->filter(($this->_request->getParam('page')));
            $search_name=$filter->filter(($this->_request->getParam('search_name')));
            if($page==""){
                      $page="1";
                           }
            $count = $user->getAllUsers('','', $search_name);
            $total = count($count);
//总页数
            $perpage = "10";
            $pages = ceil($total/$perpage);
            $start = ($page-1)*$perpage;
            $end = $perpage;

            $this->view->users = $user->getAllUsers($page, $perpage, $search_name);
            $this->view->page = $page;
            $this->view->pages = $pages;
            $this->view->total = $total;
//取出所有的group
            $groups = $group->getAllGroups();
            $count = count($groups);
            $data = array('0'=>'没有分配角色');
            for($i=0;$i<$count;$i++){
                $data[$groups[$i]['group_id']]=$groups[$i]['name'];
            }
            $this->view->roles = $data;
            echo $this->view->render('user/list.html');
      }


     public function groupAction(){
            $filter=new Zend_Filter_StripTags();
            $user = new User();
            $group = new Group();
            $user_id=$filter->filter(($this->_request->getParam('user_id')));
            
            $this->view->user = $user->getuserByuser_id($user_id);
            $user = $this->view->user;
            $group_id = $user[0]['group_id'];
            if($group_id=="0"){
                $this->view->role = "未分配角色";
            }else{
                $role = $group->getgroupByid($group_id);
                $this->view->role = $role[0]['name'];
            }
            
            $groups = $group->getAllGroups();
            $count = count($groups);
            $data = array('0'=>'没有分配角色');
            for($i=0;$i<$count;$i++){
                $data[$groups[$i]['group_id']]=$groups[$i]['name'];
            }
            
            $this->view->roles = $data;
            
            echo $this->view->render('user/group.html');
     }


     public  function editgroupAction(){
            $filter=new Zend_Filter_StripTags();
            $user = new User();

            if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
                $user_id=$filter->filter(($this->_request->getPost('user_id')));
                $group_id = $_POST['group_id'];
                $array = array('group_id'=>$group_id[0]);

                $query = $user->updateuserByuser_id($array, $user_id);
                if($query){
			echo "<script>window.location='list'</script>";
		}else{
			 echo "<script>alert('Failure!');window.location='list'</script>";
		}
            }
     }
}
?>
